package org.jeecg.modules.AccessoriesCenter.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.AccessoriesCenter.entity.LibuDaochuEntity;
import org.jeecg.modules.AccessoriesCenter.entity.LibuEntity;
import org.jeecg.modules.AccessoriesCenter.mapper.ButtonManageMapper;
import org.jeecg.modules.AccessoriesCenter.mapper.LibuManageMapper;
import org.jeecg.modules.AccessoriesCenter.service.LibuManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class LibuManageServiceImpl implements LibuManageService {

    @Autowired
    ButtonManageMapper buttonManageMapper;

    @Autowired
    LibuManageMapper mapper;
    @Override
    public IPage<LibuEntity> getLibuList(Page<LibuEntity> page, Integer status, String libuCode) {
        return mapper.getLibuList(page,status,libuCode);
    }

    @Override
    public int updateLibustatus(int id, int status) {
        return mapper.updateLibustatus( id,  status);
    }

    /**
     * 查询里布编号是否以及存在
     * @param libuCode
     * @return
     */
    @Override
    public String getlibucodebylibucode(String libuCode) {
        return mapper.getlibucodebylibucode(libuCode);
    }

    @Override
    public int addLibu(LibuEntity entity) {
        entity.setCreateTime(new Date());
        return mapper.addLibu(entity);
    }

    @Override
    public LibuEntity getLibubyId(int id) {
        return mapper.getLibubyId(id);
    }

    @Override
    public int updateLibu(LibuEntity entity) {
        return mapper.updateLibu(entity);
    }

    @Override
    public void piliangUpdatestatus(List<Integer> ids, int status) {
        for(int i=0;i<ids.size();i++){
            mapper.updateLibustatus(ids.get(i),status);
        }
    }


    @Override
    public List<LibuDaochuEntity> getLibuDaochulist(Integer status, String libuCode) {
        return mapper.getLibuDaochulist(status,libuCode);
    }

    @Override
    public int importExcel(List<LibuDaochuEntity> list) {
        if(list!=null){
            for( int i=0;i<list.size();i++){
                list.get(i).setCreateTime(new Date());

                String oldcunzai=getlibucodebylibucode( list.get(i).getLibuCode());
                //如果不存在 则执行插入操作
                if(oldcunzai==null){
                    String sysuserId=buttonManageMapper.getSysuserIdbySysuserName(list.get(i).getRealname());
                    list.get(i).setSysuserId(sysuserId);
                    if(sysuserId!=null)
                    mapper.addExcelLibu(list.get(i));
                }else {
                    String sysuserId=buttonManageMapper.getSysuserIdbySysuserName(list.get(i).getRealname());
                    list.get(i).setSysuserId(sysuserId);
                    mapper.updateExcelLibu(list.get(i));
                }

            }
        }
        return 1;
    }
}
